﻿//*************************************************************************
//面向服务的协作式智能交通系统仿真软件SoC-ITSS v2.1 alpha
//发布单位：西北工业大学--巴黎高科MINES机器人与群智能系统联合实验室
//团队负责人：张凯龙 Zhang Kailong
//团队成员：张大方、杨安胜、王敏、谢策、王雨佳、谢尘玉等
//联系我们：kl.zhang@nwpu.edu.cn
//2012-2021
//*************************************************************************


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using CyberCarSimulation.Bean;
using CyberCarSimulation.RoadModel;
//*******************************************************
//
//文件名(File Name):           HandleAlgorithm.cs
//
//数据表(Tables):              nothing 
//
//          修改日期：         2018.4.5
//          修改理由：         调度执行每一个Agent的算法
//
//      R2:
//          修改日期：         2018.11.12
//          修改理由：         实现多线程
//
//*******************************************************


namespace CyberCarSimulation.Algorithm
{
    //处理调用算法，每一个路口，执行一个算法调度
    class HandleAlgorithm
    {
        
        private List<Intersection> CEIntersectionListTemp = new List<Intersection>(); //复制的路口集合List
        private int ThreadFlag = 0;

        private Thread thread1;
        private Thread thread2;
        private Thread thread3;
        private Thread thread4;
        private Thread thread5;
        private Thread thread6;


        //无参构造函数HandleAlgorithm()
        public HandleAlgorithm()
        {           
        }

         //混合调度算法
        public void ExecuteAlgorithmMIX()
        {
            //使用单线程跑算法
            ExecuteAlgorithmSingleThread();
 
            //使用多线程跑算法
           // ExecuteAlgorithmMultiThread();
        }

        //使用单线程跑算法 混合调度算法 可以使用
        public void ExecuteAlgorithmSingleThread() 
        {           
            //将路口List复制到CEIntersectionListTemp中，接下来对CEIntersectionListTemp操作
            for (int i = 0; i < MainData.CEIntersectionList.Count; i++)
            {
                //更具每一个路口配置的不同调度策略              
                IntersectionPolicyConfig InPolicyConfig = MainData.IntersectionAlgorithmList[i];
                string AlgorithmName = InPolicyConfig.GetPolicy();

                if (AlgorithmName.Equals("FAFP-SV"))
                {
                    Intersection Intersection = MainData.CEIntersectionList[i];
                    MultiSCSFIFO_Sche MultiSCSFIFO_Sche = Intersection.GetMultiSCSFIFOSche();
                    MultiSCSFIFO_Sche.executeAlgorithm(Intersection);
                    Intersection.SetMultiSCSFIFOSche(MultiSCSFIFO_Sche); //写回路口中
                }
                else if (AlgorithmName.Equals("FAFP-MQ"))
                {
                    Intersection Intersection = MainData.CEIntersectionList[i];
                    FIFOMultiQueue_Sche FIFOMultiQueue_Sche = Intersection.GetFIFOMultiQueue_Sche();
                    FIFOMultiQueue_Sche.executeAlgorithm(Intersection,InPolicyConfig.GetAuthorization());
                    Intersection.SetFIFOMultiQueue_Sche(FIFOMultiQueue_Sche); 
                }                
                else if (AlgorithmName.Equals("FAFP-SQ")) 
                {
                    Intersection Intersection = MainData.CEIntersectionList[i];
                    FIFOSingleQueue_Sche FIFOSingleQueue_Sche = Intersection.GetFIFOSingleQueue_Sche();
                    FIFOSingleQueue_Sche.executeAlgorithm(Intersection, InPolicyConfig.GetAuthorization());
                    Intersection.SetFIFOSingleQueue_Sche(FIFOSingleQueue_Sche);
                }
               
                else if (AlgorithmName.Equals("HQEP-SV")) 
                {
                    Intersection Intersection = MainData.CEIntersectionList[i];
                    MultiSCSPrioFIFO_Sche MultiSCSPrioFIFO_Sche = Intersection.GetMultiSCSPrioFIFO_Sche();
                    MultiSCSPrioFIFO_Sche.executeAlgorithm(Intersection);
                    Intersection.SetMultiSCSPrioFIFO_Sche(MultiSCSPrioFIFO_Sche);
                }

               
                else if (AlgorithmName.Equals("HWFP-SQ-SV"))
                {
                    Intersection Intersection = MainData.CEIntersectionList[i];
                    PriorSingleQueueSingleVehicle_Sche PriorSingleQueueSingleVehicle_Sche = Intersection.GetPriorSingleQueueSingleVehicle_Sche();
                    PriorSingleQueueSingleVehicle_Sche.executeAlgorithm(Intersection, InPolicyConfig.GetAuthorization());
                    Intersection.SetPriorSingleQueueSingleVehicle_Sche(PriorSingleQueueSingleVehicle_Sche);
                }
                  
            }   
        }


        //使用多线程跑算法，调度算法函数,每一个路口
        public void ExecuteAlgorithmMultiThread()
        {
            //第一次执行
            if (ThreadFlag == 0)
            {
                InitThread();
            }
            else 
            {

            }

            //try {
            //    thread1.Resume();
            //    thread2.Resume();
            //    thread3.Resume();
            //    thread4.Resume();
            //    thread5.Resume();
            //    thread6.Resume();
            //}catch(Exception e)
            //{
            //}



            thread1.Abort();
            thread2.Abort();
            thread3.Abort();
            thread4.Abort();
            thread5.Abort();
            thread6.Abort();
        }

        //初始化线程Thread
        public void InitThread() 
        {
            //初始化留个线程
            thread1 = new Thread(ThreadMethod1);     //执行的必须是无返回值的方法
            thread2 = new Thread(ThreadMethod2);     //执行的必须是无返回值的方法
            thread3 = new Thread(ThreadMethod3);     //执行的必须是无返回值的方法
            thread4 = new Thread(ThreadMethod4);     //执行的必须是无返回值的方法
            thread5 = new Thread(ThreadMethod5);     //执行的必须是无返回值的方法
            thread6 = new Thread(ThreadMethod6);     //执行的必须是无返回值的方法

            thread1.Start();
            thread1.Join();

            thread2.Start();
            thread2.Join();

            thread3.Start();
            thread3.Join();

            thread4.Start();
            thread4.Join();

            thread5.Start();
            thread5.Join();

            thread6.Start();
            thread6.Join();
        }

        public void ExecuteThread() 
        {

        } 

        //线程一
        public void ThreadMethod1(object parameter) //方法内可以有参数，也可以没有参数
        {
            lock (MainData.__ITSLockWasTaken)             //添加lock关键字
            {
                ThreadAllMethod(0);
            }
        }

        //线程二
        public void ThreadMethod2(object parameter) //方法内可以有参数，也可以没有参数
        {
            lock (MainData.__ITSLockWasTaken)              //添加lock关键字
            {
                ThreadAllMethod(1);
            }
        }

        //线程三
        public void ThreadMethod3(object parameter) //方法内可以有参数，也可以没有参数
        {
            lock (this)             //添加lock关键字
            {
                ThreadAllMethod(2);
            }
        }

        //线程四
        public void ThreadMethod4(object parameter) //方法内可以有参数，也可以没有参数
        {
            lock (this)             //添加lock关键字
            {
                ThreadAllMethod(3);
            }
        }

        //线程五
        public void ThreadMethod5(object parameter) //方法内可以有参数，也可以没有参数
        {
            lock (this)             //添加lock关键字
            {
                ThreadAllMethod(4);
            }
        }

        //线程六
        public void ThreadMethod6(object parameter) //方法内可以有参数，也可以没有参数
        {
            lock (this)             //添加lock关键字
            {
                ThreadAllMethod(5);
            }
        }

        //多线程操作
        public void ThreadAllMethod(int i)
        {
            //更具每一个路口配置的不同调度策略              
            IntersectionPolicyConfig InPolicyConfig = MainData.IntersectionAlgorithmList[i];
            string AlgorithmName = InPolicyConfig.GetPolicy();
            if (AlgorithmName.Equals("FAFP-SV"))
            {
                lock (MainData.__ITSLockWasTaken)             //添加lock关键字
                {
                    Intersection Intersection = MainData.CEIntersectionList[i];
                    MultiSCSFIFO_Sche MultiSCSFIFO_Sche = Intersection.GetMultiSCSFIFOSche();
                    MultiSCSFIFO_Sche.executeAlgorithm(Intersection);
                    Intersection.SetMultiSCSFIFOSche(MultiSCSFIFO_Sche); //写回路口中
                }
            }
            else if (AlgorithmName.Equals("FAFP-MQ"))
            {
                lock (MainData.__ITSLockWasTaken)             //添加lock关键字
                {
                    Intersection Intersection = MainData.CEIntersectionList[i];
                    FIFOMultiQueue_Sche FIFOMultiQueue_Sche = Intersection.GetFIFOMultiQueue_Sche();
                    FIFOMultiQueue_Sche.executeAlgorithm(Intersection, InPolicyConfig.GetAuthorization());
                    Intersection.SetFIFOMultiQueue_Sche(FIFOMultiQueue_Sche);
                }
            }
            else if (AlgorithmName.Equals("FAFP-SQ"))
            {
                lock (MainData.__ITSLockWasTaken)             //添加lock关键字
                {
                    Intersection Intersection = MainData.CEIntersectionList[i];
                    FIFOSingleQueue_Sche FIFOSingleQueue_Sche = Intersection.GetFIFOSingleQueue_Sche();
                    FIFOSingleQueue_Sche.executeAlgorithm(Intersection, InPolicyConfig.GetAuthorization());
                    Intersection.SetFIFOSingleQueue_Sche(FIFOSingleQueue_Sche);
                }
            }
           
            else if (AlgorithmName.Equals("HQEP-SV"))
            {
                lock (MainData.__ITSLockWasTaken)             //添加lock关键字
                {
                    Intersection Intersection = MainData.CEIntersectionList[i];
                    MultiSCSPrioFIFO_Sche MultiSCSPrioFIFO_Sche = Intersection.GetMultiSCSPrioFIFO_Sche();
                    MultiSCSPrioFIFO_Sche.executeAlgorithm(Intersection);
                    Intersection.SetMultiSCSPrioFIFO_Sche(MultiSCSPrioFIFO_Sche);
                }
            }

            
            else if (AlgorithmName.Equals("HWFP-SQ-SV"))
            {
                lock (MainData.__ITSLockWasTaken)             //添加lock关键字
                {
                    Intersection Intersection = MainData.CEIntersectionList[i];
                    PriorSingleQueueSingleVehicle_Sche PriorSingleQueueSingleVehicle_Sche = Intersection.GetPriorSingleQueueSingleVehicle_Sche();
                    PriorSingleQueueSingleVehicle_Sche.executeAlgorithm(Intersection, InPolicyConfig.GetAuthorization());
                    Intersection.SetPriorSingleQueueSingleVehicle_Sche(PriorSingleQueueSingleVehicle_Sche);
                }
            }
            
        }


        //使用线程池跑算法，调度算法函数,每一个路口
        public void ExecuteAlgorithmThreadPool() 
        {


        }


    }
}
